home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10859 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: newsfeeds.ans.net!interaccess!usenet
  2. From: brianmcg@interaccess.com (Brian V. McGroarty)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: array of struct
  5. Date: 20 Mar 1996 14:18:44 GMT
  6. Organization: Internet Squire
  7. Message-ID: <4ip444$g6h@nntp.interaccess.com>
  8. References: <Pine.LNX.3.91.960319173746.16221A-100000@larry.inf.net>
  9. Reply-To: brianmcg@interaccess.com
  10. NNTP-Posting-Host: d41-isdn.nhe.interaccess.com
  11. X-Newsreader: Internet Squire 1.20
  12.  
  13. I'll assume that pointers to the strings are okay, i.e. you don't need them
  14. to be an actual part of the structure.
  15.  
  16. I would allocate a pool of RAM which is one character larger than the file.
  17.  By running through this range of memory and replacing all line ends with
  18. character 0 you create a table of individual strings.  During the run
  19. through the file where you were replacing line ends you could either count
  20. the number of strings to determine how many structures you will need to
  21. allocate in an array, or you could count as well as place pointers to the
  22. beginning of each string in a temporary array of pointers which is
  23. guaranteed to be large enough to hold the entire set of pointers.  If you
  24. have simply counted then you must run through the file again to fill the
  25. member of each structure which should contain a pointer to a string.  If
  26. you have created the temporary array of pointers then you would copy these
  27. pointers from the array instead.
  28.  
  29. Something else which I see people do from time to time is to allocate an
  30. array of items which is guaranteed to be large enough to hold a full set
  31. and resize with realloc() or to create a linked list, resulting in a new
  32. allocation for every single record.  I don't like the realloc() method
  33. because it temporarily bloats your program's memory requirements and, thus,
  34. causes some virtual memory systems to thrash.  A linked list is a good
  35. answer if you will be dynamically adding or removing data items, but it
  36. reduces the speed of random access to your data and adds extra
  37. inefficiencies due to the system's internal structuring of your memory list
  38. and on systems which allocate objects of a multiple of a certain size.
  39.  
  40.  
  41. Lawrence O'Leary wrote:
  42.  
  43. >I have a question... though it is petty it bothers me greatly.  :(
  44.  
  45. >How would I create a runtime defined array of struct.
  46.  
  47. >What I want to do is create each record in the array by reading a line 
  48. >from a file and storing each field of the line into the record.  Do this 
  49. >for every line in the file and then where done.  What I need to do is 
  50. >create the array of the struct for the exact number of lines that are in 
  51. >the file.  This way I won't be wasting memory and I won't have to worry 
  52. >about not having enough records to store the lines into.
  53.  
  54. ---
  55. Brian Valters McGroarty -- brianmcg@bix.com
  56. phone/fax (847) 439-7714
  57.